package game;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.prefs.Preferences;

/**
 *
 * @author Luan
 */
public class Configurations {
    
    public static String DATA_PATH, LAST_PROFILE, TEXTURE_PACK;
    public static int SCREEN_WIDTH, SCREEN_HEIGHT, GAME_HEIGHT, BLOCK_SIZE, LANGUAGE;
    public static double BRIGHTNESS;
    public static boolean FULLSCREEN;
    
    public static final String[] DEFAULT_NAMES = {"DATA_PATH", "SCREEN_WIDTH", "GAME_HEIGHT", "SCREEN_HEIGHT", "FULL_SCREEN", "BLOCK_SIZE", "BRIGHTNESS", "LAST_PROFILE", "TEXTURE_PACK", "LANGUAGE"};
    public static final String DEFAULT_DP = "Saved Games\\Terent", DEFAULT_LP = "", DEFAULT_TP = "default";
    public static final int DEFAULT_SW = 816, DEFAULT_SH = 742, DEFAULT_GH = 592, DEFAULT_BS = 16, DEFAULT_LANG = 0;
    public static final boolean DEFAULT_FS = false;
    public static final double DEFAULT_BRIGHTNESS = 0.5d;

    private static final Preferences prefs;
    static {
        PrintStream err = System.err;
        System.setErr(new PrintStream(new OutputStream() {
            @Override public void write(int b) throws IOException {}
        }));
        prefs = Preferences.systemNodeForPackage(Configurations.class);
        System.setErr(err);
    }
    
    public static void load() {
        DATA_PATH = prefs.get(DEFAULT_NAMES[0], DEFAULT_DP);
        SCREEN_WIDTH = prefs.getInt(DEFAULT_NAMES[1], DEFAULT_SW);
        GAME_HEIGHT = prefs.getInt(DEFAULT_NAMES[2], DEFAULT_GH);
        SCREEN_HEIGHT = prefs.getInt(DEFAULT_NAMES[3], DEFAULT_SH);
        FULLSCREEN = prefs.getBoolean(DEFAULT_NAMES[4], DEFAULT_FS);
        BLOCK_SIZE = prefs.getInt(DEFAULT_NAMES[5], DEFAULT_BS);
        BRIGHTNESS = prefs.getDouble(DEFAULT_NAMES[6], DEFAULT_BRIGHTNESS);
        LAST_PROFILE = prefs.get(DEFAULT_NAMES[7], DEFAULT_LP);
        TEXTURE_PACK = prefs.get(DEFAULT_NAMES[8], DEFAULT_TP);
        LANGUAGE = prefs.getInt(DEFAULT_NAMES[9], DEFAULT_LANG);
    }
    
    public static void reset() {
        DATA_PATH = DEFAULT_DP;
        SCREEN_WIDTH = DEFAULT_SW;
        GAME_HEIGHT = DEFAULT_GH;
        SCREEN_HEIGHT = DEFAULT_SH;
        FULLSCREEN = DEFAULT_FS;
        BLOCK_SIZE = DEFAULT_BS;
        BRIGHTNESS = DEFAULT_BRIGHTNESS;
        LAST_PROFILE = DEFAULT_LP;
        TEXTURE_PACK = DEFAULT_TP;
        LANGUAGE = DEFAULT_LANG;
    }
    
    public static void save() {
        prefs.put(DEFAULT_NAMES[0], DATA_PATH);
        prefs.putInt(DEFAULT_NAMES[1], SCREEN_WIDTH);
        prefs.putInt(DEFAULT_NAMES[2], GAME_HEIGHT);
        prefs.putInt(DEFAULT_NAMES[3], SCREEN_HEIGHT);
        prefs.putBoolean(DEFAULT_NAMES[4], FULLSCREEN);
        prefs.putInt(DEFAULT_NAMES[5], BLOCK_SIZE);
        prefs.putDouble(DEFAULT_NAMES[6], BRIGHTNESS);
        prefs.put(DEFAULT_NAMES[7], LAST_PROFILE);
        prefs.put(DEFAULT_NAMES[8], TEXTURE_PACK);
        prefs.putInt(DEFAULT_NAMES[9], LANGUAGE);
    }
    
    public static String getDataPath() {
        return System.getProperty("user.home") + "\\" + DATA_PATH;
    }
}
